﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace CoreWeb.Utility.Filter
{
    /// <summary>
    /// OnResourceExecuted  保存缓存
    /// OnResourceExecuting使用缓存
    /// </summary>
    public class CustomCacheResultFilterAttribute : Attribute, IResultFilter, IFilterMetadata, IOrderedFilter
    {
        public int Duration {  get; set; } 
           public int Order => 0;

        public void OnResultExecuted(ResultExecutedContext context)
        {
            //不能放这里  已经指定了response
        }

        public void OnResultExecuting(ResultExecutingContext context)
        {
            context.HttpContext.Response.Headers["Cache-Control"] = $"public,max-age={this.Duration}";
        }
    }
}
